package org.qphoto;

import java.util.Date;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

public class PrePostActivity extends Activity {

	String   tmpimagepath = null;
	String   strcontent   = null;
	int      picindex     = 0;
	int[] selected        = null;
	Bitmap[] bmpbuffers   = null;
	ImageView gifImageView= null;
	Button btnCancel      = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prepost);
		
		Bundle bunde    = this.getIntent().getExtras();
	    tmpimagepath    = bunde.getString("tmpimagepath");
	    strcontent      = bunde.getString("postcontent");
	    selected        = bunde.getIntArray("selectedimages");
	    bmpbuffers      = new Bitmap[selected.length];
	    for(int i=0;i<selected.length;i++)
	    {
	    	bmpbuffers[i] = null;
	    }
		
	    int sampleimgid        = selected[0];
	    String sampleimagepath = tmpimagepath + "/" + sampleimgid + ".jpg";
	    Bitmap picimage        = BitmapFactory.decodeFile(sampleimagepath);
	    gifImageView = (ImageView) this.findViewById(R.id.sampleimage);

	    /*
	    if ( picimage != null)
	    {
	    	gifImageView.setImageBitmap(picimage);
		    bmpbuffers[0] = picimage;
	    }
	    */
	    
	    if ( picimage != null)
		{
			int bmpWidth  = picimage.getWidth();
			int bmpHeight = picimage.getHeight();
			int vWidth    = gifImageView.getLayoutParams().width;
			if (bmpWidth < vWidth) {
				gifImageView.setScaleType(ScaleType.CENTER);
				gifImageView.getLayoutParams().height = bmpHeight;
			} else {
				gifImageView.setScaleType(ScaleType.FIT_CENTER);
				gifImageView.getLayoutParams().height = bmpHeight * vWidth
						/ bmpWidth;
			}
			gifImageView.setImageBitmap(picimage);
			bmpbuffers[0] = picimage;
		}
	    
		TextView postTextView = (TextView)this.findViewById(R.id.postcontent);
		postTextView.setText( strcontent );
		
		Button   btnPostMsg   = (Button) this.findViewById(R.id.postmessage);
		btnPostMsg.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				PostMessage();
			}
		});
		
		btnCancel = (Button)this.findViewById(R.id.cancel);
        btnCancel.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PrePostActivity.this.finish();
			}
		});
		
		handler.postDelayed(runnable,100);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		for(int i=0;i<bmpbuffers.length;i++)
		{
			if ( bmpbuffers[i] != null)
			{
				bmpbuffers[i].recycle();
			}
		}
	}
	
	Handler handler   = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			picindex++;
			if ( picindex >= selected.length)
				picindex = 0;
			
			Bitmap bmp = null;
			if ( bmpbuffers[picindex] != null)
			{
				bmp = bmpbuffers[picindex];
			} else {
				String sampleimagepath =  tmpimagepath + "/" + picindex + ".jpg";
				Bitmap picimage        = BitmapFactory.decodeFile(sampleimagepath);
				bmpbuffers[picindex]   = picimage;
				bmp                    = picimage;
			}
			gifImageView.setImageBitmap(bmp);
			
			handler.postDelayed(runnable,100);
		}
	};
	
	public void PostMessage()
	{
		/*
        UserInfoDBHelper userdb = CoreData.getCoreData().getUserInfoDBHelper();
        MessageDBHelper  msgdb  = CoreData.getCoreData().getMessageDBHelper();
		UserInfo userinfo       = CoreData.getCoreData().getLoginuser();
		
		// data
		Message message = new Message();
		message.setUserid(userinfo.getUserid());
        message.setBreply(false);
        message.setContent(strcontent);
        message.setImageuri(HandleFiles());
        
        //long msgid = msgdb.PostMessage(message);
        
       
        Toast.makeText(PrePostActivity.this,
				R.string.strPostOk,
				Toast.LENGTH_SHORT).show();
		*/
        Bundle bundle = new Bundle();
        bundle.putInt("type",1);
		bundle.putString("content",strcontent);
		bundle.putString("respath",HandleFiles());
        
        Intent showNextPage_Intent=new Intent();
        showNextPage_Intent.putExtras(bundle); 
		showNextPage_Intent.setClass(PrePostActivity.this,SendDialog.class);
		startActivity(showNextPage_Intent);
	}
	
	private String HandleFiles()
	{
		int num          = selected.length;
		String photopath = CoreData.getCoreData().getImagePath();
		String filename  = getRandomFileName();
		for(int i=0;i<num;i++)
		{
			int sampleimgid = selected[i];
			String orgfile  = tmpimagepath + "/" + sampleimgid + ".jpg";
			String realfile = photopath + "/" + filename + "_" + i + ".jpg";
			
			FileAccess.Copy(orgfile, realfile);
		}
		int maxindex = num -1;
		return photopath + "/" + filename + "_" + maxindex + ".jpg";
	}
	
	public static String getRandomFileName()
	{
		StringBuilder sb = new StringBuilder();
		Date date        = new Date();
		sb.append( date.getTime() );
		Random random    = new Random();
		sb.append( Math.abs(random.nextInt()) );
		return sb.toString();
	}
}
